package 反射3;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 通过反射访问构造器(构造方法)
 * getConstructor()它的参数不是方法名，而是构造函数的参数类型列表,通过参数类型定位是哪个构造函数
 */
public class TestUser {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<User> userClass = User.class;
//        User user = userClass.newInstance();不用创建类对象
        Constructor<User> constructor = userClass.getConstructor(int.class);
        User user1 = constructor.newInstance(20);
        System.out.println("user1 = " + user1);

        Constructor<User> constructor1 = userClass.getConstructor(int.class, String.class);
        User user2 = constructor1.newInstance(46,"李四");
        System.out.println("user2 = " + user2);
    }
}
